route.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { NextResponse } from "next/server";
  2. import automation from "data/automation.json";
  3. const get = (url) => {
  4. return fetch(url, { cache: "no-store" })
  5. .then((resp) => resp && resp.text())
  6. .then((result) => result);
  7. };
  8. const update = ({ id, client, ...rest }) => {
  9. let url = `http://${client}/win`;
  10. if (id) url += `&PL=${id}`;
  11. return get(url)
  12. .then((resp) => ({
  13. ps: id
  14. }))
  15. .catch((err) => ({ error: err.message }));
  16. };
  17. export async function GET(req, { params }) {
  18. let promises = [];
  19. let id = params?.id || null;
  20. try {
  21. if (!id) throw new Error("No id specified");
  22. let clients = (automation?.[1] && Object.keys(automation?.[1])) || [];
  23. if (!clients) throw new Error("No clients for id", id);
  24. for (let client of clients) {
  25. promises.push(update({ id, client }));
  26. }
  27. } catch (err) {
  28. return NextResponse.json({ error: err?.message }, { status: 500 });
  29. }
  30. return Promise.allSettled(promises)
  31. .then((results) => {
  32. return NextResponse.json(results?.map((o) => o?.value));
  33. })
  34. .catch((err) => {
  35. return NextResponse.json({ error: err?.message }, { status: 500 });
  36. });
  37. }